package com.example.firstapp

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.example.firstapp.ContentProvider.ContentProviderActivity
import com.example.firstapp.broadcast.broadcastActivity
import com.example.firstapp.databinding.ActivityMainBinding
import com.example.firstapp.fragment.fragmentActivity
import com.example.firstapp.layout.*
import com.example.firstapp.layout.UIBestPractice.ChatActivity
import com.example.firstapp.layout.recyclerView.recyclerviewActivity
import com.example.firstapp.materialDesign.materialDesignActivity
import com.example.firstapp.network.NetworkActivity
import com.example.firstapp.notice.NoticeActivity
import com.example.firstapp.service.ServiceActivity
import com.example.firstapp.storeData.StoreDataActivity


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val bt: Button = findViewById(R.id.button)
        bt.setOnClickListener {
            Toast.makeText(this, "点击了按钮", Toast.LENGTH_SHORT).show()
        }

        // 显示调用
        val bt_intent1:Button = findViewById(R.id.intent1)
        bt_intent1.setOnClickListener {
            val intent = Intent(this, intent1Activity::class.java)
            startActivity(intent)
        }
        // 隐式intent
        val bt_intent2:Button = findViewById(R.id.intent2)
        bt_intent2.setOnClickListener {
            val intent = Intent("com.example.intent2.ACTION_START")
            startActivity(intent)
        }
        // 隐式intent 启动百度
        val bt_intent3:Button = findViewById(R.id.intent3)
        bt_intent3.setOnClickListener {
            // Intent.ACTION_VIEW是内置动作
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }
        // 隐式intent 拨打电话
        val bt_intent4:Button = findViewById(R.id.intent4)
        bt_intent4.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        }

        // 1 注册回调
        val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode  == RESULT_OK) {
                val returnData = result.data?.getStringExtra("return_data")
                val ed:TextView = findViewById(R.id.tv_result);  // 替换为你TextView的id
                ed.append(returnData)
            }
        }
        // 传递数据给下一个activity444生十三
        binding.intent5.setOnClickListener {
            val intent = Intent(this,intent4Activity::class.java)
            val data1 = findViewById<EditText>(R.id.editTextText1).getText().toString()

            //val ed:EditText = findViewById(R.id.editTextText1)
            //val data1 = ed.getText().toString()
            intent.putExtra("send_data1", data1)
            // 2、期望新启动Activity在销毁的时候能够返回一个结果
            // startActivityForResult(intent, 1)  已启用
//            intent.putExtra("extra_data", data)  不回调只传递给下一个activity
//            startActivity(intent)
            resultLauncher.launch(intent)
        }

        val bt_intent6:Button = findViewById(R.id.intent6)
        bt_intent6.setOnClickListener {
            val intent = Intent(this,lifecycleActivity::class.java)
            startActivity(intent)
        }
        val bt_layout:Button = findViewById(R.id.intent2Layout)
        bt_layout.setOnClickListener {
            val intent = Intent(this, layoutActivity::class.java)
            startActivity(intent)
        }
        val bt_listview:Button = findViewById(R.id.intent2Listview)
        bt_listview.setOnClickListener {
            val intent = Intent(this, listviewActivity::class.java)
            startActivity(intent)
        }
        val bt_cyclerview:Button = findViewById(R.id.intent2Recyclerview)
        bt_cyclerview.setOnClickListener {
            val intent = Intent(this, recyclerviewActivity::class.java)
            startActivity(intent)
        }
        val bt_Chat:Button = findViewById(R.id.intent2Chat)
        bt_Chat.setOnClickListener {
            val intent = Intent(this, ChatActivity::class.java)
            startActivity(intent)
        }

        val bt_fragment:Button = findViewById(R.id.intent2fragment)
        bt_fragment.setOnClickListener {
            val intent = Intent(this, fragmentActivity::class.java)
            startActivity(intent)
        }
        val intent2broadcast:Button = findViewById(R.id.intent2broadcast)
        intent2broadcast.setOnClickListener {
            val intent = Intent(this, broadcastActivity::class.java)
            startActivity(intent)
        }

        binding.intent2StoreData.setOnClickListener {
            val intent = Intent(this, StoreDataActivity::class.java)
            startActivity(intent)
        }

        binding.intent2ContentProvider.setOnClickListener {
            val intent = Intent(this, ContentProviderActivity::class.java)
            startActivity(intent)
        }
        binding.intent2Noice.setOnClickListener {
            val intent = Intent(this, NoticeActivity::class.java)
            startActivity(intent)
        }
        binding.intent2Service.setOnClickListener {
            val intent = Intent(this, ServiceActivity::class.java)
            startActivity(intent)
        }
        binding.intent2network.setOnClickListener {
            val intent = Intent(this, NetworkActivity::class.java)
            startActivity(intent)
        }
        binding.materialDesign.setOnClickListener {
            val intent = Intent(this, materialDesignActivity::class.java)
            startActivity(intent)
        }

    }
    //接收返回的数据  已弃用
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        Log.d("main_activity",requestCode.toString())
//        when (requestCode) {
//            1 -> if (resultCode == RESULT_OK) {
//                val returnedData = data?.getStringExtra("data_return")
//                val ed:TextView = findViewById(R.id.tv_result);  // 替换为你TextView的id
//                ed.append(returnedData)
//            }
//        }
//    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu1,menu)
        return true
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.item1 -> Toast.makeText(this, "You clicked Add",
                Toast.LENGTH_SHORT).show()
            R.id.item2 -> Toast.makeText(this, "You clicked Remove",
                Toast.LENGTH_SHORT).show()
        }
        return true
    }
}